package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBCharacter extends SQLiteOpenHelper {

	public DBCharacter(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE characters ( charname TEXT PRIMARY KEY, level TEXT, " +
					   "classtype TEXT, type TEXT, xp TEXT, hp TEXT, mp TEXT, stamina TEXT, " + 
					   "intelect TEXT, strength TEXT, agility TEXT, karma TEXT, " +
					   "mapname TEXT, posx TEXT, posy TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBCharacter:","TABLE characters SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS characters";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBCharacter:","UPDATE SUCCESSFUL!");
	}
	
	public void insertCharacter(HashMap<String ,String> characterValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", characterValues.get("charname"));
		values.put("level", characterValues.get("level"));
		values.put("classtype", characterValues.get("classtype"));
		values.put("type", characterValues.get("type"));
		values.put("xp", characterValues.get("xp"));
		values.put("hp", characterValues.get("hp"));
		values.put("mp", characterValues.get("mp"));
		values.put("stamina", characterValues.get("stamina"));
		values.put("intelect", characterValues.get("intelect"));
		values.put("strength", characterValues.get("strength"));
		values.put("agility", characterValues.get("agility"));
		values.put("karma", characterValues.get("karma"));
		values.put("mapname", characterValues.get("mapname"));
		values.put("posx", characterValues.get("posx"));
		values.put("posy", characterValues.get("posy"));
		
		db.insert("characters", null, values);
		db.close();
		
		Log.i("DATABASE DBCharacter:","CHARACTER INSERT WAS SUCCESSFUL!");
	}
	
	public int updateCharacter(HashMap<String ,String> characterValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", characterValues.get("charname"));
		values.put("level", characterValues.get("level"));
		values.put("classtype", characterValues.get("classtype"));
		values.put("type", characterValues.get("type"));
		values.put("xp", characterValues.get("xp"));
		values.put("hp", characterValues.get("hp"));
		values.put("mp", characterValues.get("mp"));
		values.put("stamina", characterValues.get("stamina"));
		values.put("intelect", characterValues.get("intelect"));
		values.put("strength", characterValues.get("strength"));
		values.put("agility", characterValues.get("agility"));
		values.put("karma", characterValues.get("karma"));
		values.put("mapname", characterValues.get("mapname"));
		values.put("posx", characterValues.get("posx"));
		values.put("posy", characterValues.get("posy"));
		
		Log.i("DATABASE DBCharacter:","CHARACTER UPDATE WAS SUCCESSFUL!");
		
		return db.update("characters", values, "charname" + "?=", new String[] {characterValues.get("charname")});
	}
	
	public void deleteCharacter(String charname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM characters WHERE charname='" + charname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBCharacter:","CHARACTER DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getCharacters() {
		
		ArrayList<HashMap<String, String>> characterList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM characters";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> characters = new HashMap<String, String>();
				
				characters.put("charname", cursor.getColumnName(0));
				characters.put("level", cursor.getColumnName(1));
				characters.put("classtype", cursor.getColumnName(2));
				characters.put("type", cursor.getColumnName(3));
				characters.put("xp", cursor.getColumnName(4));
				characters.put("hp", cursor.getColumnName(5));
				characters.put("mp", cursor.getColumnName(6));
				characters.put("stamina", cursor.getColumnName(7));
				characters.put("intelect", cursor.getColumnName(8));
				characters.put("strength", cursor.getColumnName(9));
				characters.put("agility", cursor.getColumnName(10));
				characters.put("karma", cursor.getColumnName(11));
				characters.put("mapname", cursor.getColumnName(12));
				characters.put("posx", cursor.getColumnName(13));
				characters.put("posy", cursor.getColumnName(14));
				
				characterList.add(characters);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBCharacter:","CHARACTERS RETURN WAS SUCCESSFUL!");
		
		return characterList;
	}
	
	public HashMap<String, String> getCharacter(String charname){
		
		HashMap<String, String> characters = new HashMap<String, String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM characters WHERE charname='" + charname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				characters.put("charname", cursor.getColumnName(0));
				characters.put("level", cursor.getColumnName(1));
				characters.put("classtype", cursor.getColumnName(2));
				characters.put("type", cursor.getColumnName(3));
				characters.put("xp", cursor.getColumnName(4));
				characters.put("hp", cursor.getColumnName(5));
				characters.put("mp", cursor.getColumnName(6));
				characters.put("stamina", cursor.getColumnName(7));
				characters.put("intelect", cursor.getColumnName(8));
				characters.put("strength", cursor.getColumnName(9));
				characters.put("agility", cursor.getColumnName(10));
				characters.put("karma", cursor.getColumnName(11));
				characters.put("mapname", cursor.getColumnName(12));
				characters.put("posx", cursor.getColumnName(13));
				characters.put("posy", cursor.getColumnName(14));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBCharacter:","CHARACTER RETURN WAS SUCCESSFUL!");
		
		return characters;
	}
}

